# -*- coding: utf-8 -*-
import uuid

from sqlalchemy import JSON, Column, String, Boolean, Integer, ForeignKey, Float, Text
from sqlalchemy.orm import relationship

from db.base import Base
from db.models.base import BaseDatabaseModel, GUID


class AccidentCaseFile(BaseDatabaseModel, Base):
    __tablename__ = "ac_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    hash_name = Column(String(255), nullable=True, comment="哈希文件名")
    name = Column(String(255), nullable=True, comment="文件名")
    file_type = Column(String(32), nullable=True, comment="文件类型")
    file_size = Column(Float, nullable=True, comment="文件大小")
    location = Column(String(512), nullable=True, comment="存储位置")

    accident_level = Column(String(32), nullable=True, comment="事故等级")
    report_date = Column(String(32), nullable=True, comment="报告日期")
    related_company = Column(String(32), nullable=True, comment="涉及公司")
    accident_abstract = Column(String(32), nullable=True, comment="事故摘要")

    is_delete = Column(Boolean, default=False, index=True)
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    accident_type = Column(JSON, nullable=True, default=[], comment="事故类型")
    group_ids = Column(JSON, nullable=True, default=[])  # 存储可以访问的用户组ID列表，以逗号分隔
    

    def check_permission(self, user, user_groups):
        if self.owner == user.id:
            return True
        if any(group_id in self.group_ids for group_id in user_groups):
            return True
        return False

    def to_json(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "hash_name": self.hash_name,
            "location": self.location,
            "type": self.file_type,
            "size": self.file_size,
            "is_delete": self.is_delete,
            "group_ids": self.group_ids,
            "accident_level": self.accident_level,
            "report_date": self.report_date,
            "related_company": self.related_company,
            "accident_abstract": self.accident_abstract,
            "accident_type": self.accident_type,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "create_time", None) else None,
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "updated_time", None) else None,
        }


class DualControlFile(BaseDatabaseModel, Base):
    __tablename__ = "dc_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    name = Column(String(255), nullable=True, comment="文件名")
    hash_name = Column(String(255), nullable=True, comment="哈希文件名")
    location = Column(String(512), nullable=True, comment="存储位置")
    file_type = Column(String(32), nullable=True, comment="文件类型")
    file_size = Column(Float, nullable=True, comment="文件大小")
    is_delete = Column(Boolean, default=False, index=True)

    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    group_ids = Column(JSON, nullable=True, default=[])  # 存储可以访问的用户组ID列表，以逗号分隔

    def check_permission(self, user, user_groups):
        if self.owner == user.id:
            return True
        if any(group_id in self.group_ids for group_id in user_groups):
            return True
        return False

    def to_json(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "hash_name": self.hash_name,
            "location": self.location,
            "type": self.file_type,
            "size": self.file_size,
            "is_delete": self.is_delete,
            "group_ids": self.group_ids,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "create_time", None) else None,
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "updated_time", None) else None,
        }


class EmergencyFile(BaseDatabaseModel, Base):
    __tablename__ = "emergency_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    hash_name = Column(String(255), nullable=True, comment="哈希文件名")
    name = Column(String(255), nullable=True, comment="文件名")
    file_type = Column(String(32), nullable=True, comment="文件类型")
    file_size = Column(Float, nullable=True, comment="文件大小")
    location = Column(String(512), nullable=True, comment="存储位置")

    scene_type = Column(JSON, nullable=True, default=[], comment="场景类型")
    group_ids = Column(JSON, nullable=True, default=[])  # 存储可以访问的用户组ID列表，以逗号分隔

    is_delete = Column(Boolean, default=False, index=True)
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    accident_type = Column(JSON, nullable=True, default=[], comment="事故类型")

    def check_permission(self, user, user_groups):
        if self.owner == user.id:
            return True
        if any(group_id in self.group_ids for group_id in user_groups):
            return True
        return False

    def to_json(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "hash_name": self.hash_name,
            "location": self.location,
            "type": self.file_type,
            "size": self.file_size,
            "is_delete": self.is_delete,
            "group_ids": self.group_ids,
            "scene_type": self.scene_type,
            "accident_type": self.accident_type,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "create_time", None) else None,
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "updated_time", None) else None,
        }


class QAFile(BaseDatabaseModel, Base):
    __tablename__ = "qa_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    hash_name = Column(String(255), nullable=True, comment="哈希文件名")
    name = Column(String(255), nullable=True, comment="文件名")
    file_type = Column(String(32), nullable=True, comment="文件类型")
    file_size = Column(Float, nullable=True, comment="文件大小")
    location = Column(String(512), nullable=True, comment="存储位置")

    gb_num = Column(String(64), nullable=True, default="", comment="适用范围")
    applicable_scope = Column(String(512), nullable=True, default="", comment="适用范围")

    is_delete = Column(Boolean, default=False, index=True)
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")

    attributes = relationship("FileAttributeAssociation", backref="file", cascade="all, delete-orphan")

    # 添加便捷属性访问方法
    @property
    def applicable_objects(self):
        return [assoc for assoc in self.attributes if assoc.attribute_type == "applicable_object"]

    @property
    def knowledge_types(self):
        return [assoc for assoc in self.attributes if assoc.attribute_type == "knowledge_type"]

    @property
    def applicable_areas(self):
        return [assoc for assoc in self.attributes if assoc.attribute_type == "applicable_area"]

    @property
    def element_classifications(self):
        return [assoc for assoc in self.attributes if assoc.attribute_type == "element_classification"]

    def to_json(self):
        return {
            "name": self.name,
            "hash_name": self.hash_name,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "id": str(self.id),
            "type": self.file_type,
            "size": self.file_size,
            "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S"),
            "applicable_objects": [str(a.attribute_id) for a in self.applicable_objects],
            "knowledge_types": [str(a.attribute_id) for a in self.knowledge_types],
            "applicable_areas": [str(a.attribute_id) for a in self.applicable_areas],
            "element_classifications": [str(a.attribute_id) for a in self.element_classifications],
            "applicable_scope": self.applicable_scope,
        }


class AudioFile(BaseDatabaseModel, Base):
    __tablename__ = "audio_files"

    id = Column(GUID(), primary_key=True, index=True, default=uuid.uuid1)
    hash_name = Column(String(255), nullable=True, comment="哈希文件名")
    name = Column(String(255), nullable=True, comment="文件名")
    file_type = Column(String(32), nullable=True, comment="文件类型")
    file_size = Column(Float, nullable=True, comment="文件大小")
    location = Column(String(512), nullable=True, comment="存储位置")
    status = Column(String(32), nullable=True, comment="状态")
    is_delete = Column(Boolean, default=False, index=True, comment="是否删除")
    created_by = Column(Integer, ForeignKey('users.id'), comment="创建人")
    company_name = Column(String(255), nullable=True, comment="公司名称")
    group_name = Column(String(255), nullable=True, comment="组名称")
    content = Column(Text, nullable=True, default="", comment="内容")
    summary = Column(Text, nullable=True, default="", comment="摘要")
    keywords = Column(JSON, nullable=True, default=[], comment="关键词")
    comment = Column(String(255), nullable=True, default="", comment="备注")

    group_ids = Column(JSON, nullable=True, default=[])  # 存储可以访问的用户组ID列表，以逗号分隔

    def check_permission(self, user, user_groups):
        if self.owner == user.id:
            return True
        if any(group_id in self.group_ids for group_id in user_groups):
            return True
        return False
    
    def to_json(self):
        return {
            "id": str(self.id),
            "name": self.name,
            "hash_name": self.hash_name,
            # "location": self.location,
            # "type": self.file_type,
            "size": self.file_size,
            "status": self.status,
            # "is_delete": self.is_delete,
            "group_ids": self.group_ids,
            "company_name": self.company_name,
            "group_name": self.group_name,
            "content": self.content,
            "summary": self.summary,
            "keywords": self.keywords,
            # "comment": self.comment,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "create_time", None) else None,
            # "updated_time": self.updated_time.strftime("%Y-%m-%d %H:%M:%S") if getattr(self, "updated_time", None) else None,
        }
